<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :form="true"
    :destroy-on-close="true"
    :width="460"
    v-model="visible"
    :title="isUpdate ? '修改团队分析' : '添加团队分析'"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      @submit.prevent=""
    >
      <el-form-item label="所属组织" prop="deptId">
        <el-input
          clearable
          v-model="form.deptId"
          placeholder="请输入所属组织"
        />
      </el-form-item>
      <el-form-item label="员工数" prop="employeeCount">
        <el-input
          clearable
          v-model="form.employeeCount"
          placeholder="请输入员工数"
        />
      </el-form-item>
      <el-form-item label="客户数" prop="customerCount">
        <el-input
          clearable
          v-model="form.customerCount"
          placeholder="请输入客户数"
        />
      </el-form-item>
      <el-form-item label="录音数" prop="recordingCount">
        <el-input
          clearable
          v-model="form.recordingCount"
          placeholder="请输入录音数"
        />
      </el-form-item>
      <el-form-item label="有效录音数" prop="validRecordingCount">
        <el-input
          clearable
          v-model="form.validRecordingCount"
          placeholder="请输入有效录音数"
        />
      </el-form-item>
      <el-form-item label="工牌使用率" prop="badgeUsageRate">
        <el-input
          clearable
          v-model="form.badgeUsageRate"
          placeholder="请输入工牌使用率"
        />
      </el-form-item>
      <el-form-item label="服务平均分" prop="avgServiceScore">
        <el-input
          clearable
          v-model="form.avgServiceScore"
          placeholder="请输入服务平均分"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input
          clearable
          v-model="form.createdBy"
          placeholder="请输入创建人"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker
          v-model="form.createdTime"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间"
          class="ele-fluid"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input
          clearable
          v-model="form.updateBy"
          placeholder="请输入修改人"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker
          v-model="form.updateTime"
          value-format="YYYY-MM-DD"
          placeholder="请选择修改时间"
          class="ele-fluid"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { EleMessage } from 'ele-admin-plus';
  import { useFormData } from '@/utils/use-form-data';
  import { addEtpOrganizationStats, updateEtpOrganizationStats } from '@/api/etp/etpOrganizationStats';

  const props = defineProps({
    /** 修改回显的数据 */
    data: Object
  });

  const emit = defineEmits(['done']);

  /** 弹窗是否打开 */
  const visible = defineModel({ type: Boolean });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    id: void 0,
    deptId: void 0,
    employeeCount: void 0,
    customerCount: void 0,
    recordingCount: void 0,
    validRecordingCount: void 0,
    badgeUsageRate: '',
    avgServiceScore: void 0,
    createdBy: '',
    createdTime: '',
    updateBy: '',
    updateTime: ''
  });

  /** 表单验证规则 */
  const rules = reactive({
  });

  /** 关闭弹窗 */
  const handleCancel = () => {
    visible.value = false;
  };

  /** 保存编辑 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      const saveOrUpdate = isUpdate.value ? updateEtpOrganizationStats : addEtpOrganizationStats;
      saveOrUpdate(form)
        .then((msg) => {
          loading.value = false;
          EleMessage.success({ message: msg, plain: true });
          handleCancel();
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error({ message: e.message, plain: true });
        });
    });
  };

  /** 监听弹窗打开 */
  watch(visible, () => {
    if (visible.value) {
      if (props.data) {
        assignFields(props.data);
        isUpdate.value = true;
      } else {
        resetFields();
        isUpdate.value = false;
      }
    }
  });
</script>
